package com.haojiu.controller;

import com.github.pagehelper.PageInfo;
import com.haojiu.pojo.Book;
import com.haojiu.pojo.LendList;
import com.haojiu.pojo.User;
import com.haojiu.service.BookService;
import com.haojiu.service.LendListService;
import com.haojiu.service.NoticeService;
import com.haojiu.util.ExportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author:我叫浩九
 * @date：2021/8/30 11:50
 * create in RuGao JiangSu
 */
@Controller
public class LendListController {
    @Autowired
    @Qualifier("LendListServiceImpl")
    private LendListService lendListService;
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    @RequestMapping("/lendListIndex")
    public ModelAndView queryAllUser(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        List<LendList> list = lendListService.getAllLendList(page, size);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        for (LendList lendList : list) {
            Date date1=lendList.getBackDate();
            if(date.compareTo(date1)>0){
                lendList.setStatus("未按时还书");
            }
        }
        PageInfo<LendList> pageInfo = new PageInfo<>(list);
        mv.addObject("pageInfo", pageInfo);
        HttpSession session = request.getSession();
        String userType = (String) session.getAttribute("userType");
        if (userType.equals("manager")) {
            mv.setViewName("lendList/allLend");
        } else {
            mv.setViewName("lendList/allLend2");
        }
        return mv;
    }

    @RequestMapping("/login/delLend/{lendId}/{bookName}")
    public String deleteLend(@PathVariable("lendId") int lendId,@PathVariable("bookName") String bookName, Model model) {
        lendListService.backBook(lendId);
        String str = "还书成功";
        Book book = bookService.queryBookByName(bookName);
        Book book1 = new Book(book.getBookID(), book.getBookName(), book.getBookCounts() + 1, book.getAuthor(), book.getBookType(), book.getPrice(), book.getBookLanguage());
       bookService.updateBook(book1);
        model.addAttribute("str",str);
        return "redirect:/login/backBook";
    }


    @RequestMapping("/login/toAddLend")
    public String toAddLend() {
        return "lendList/addLend";
    }

    @RequestMapping("/login/addLend")
    public String addLend(String bookName,@RequestParam(defaultValue = "7") int LendTime, HttpServletRequest request,Model model) throws ParseException {
        Book book = bookService.queryBookByName(bookName);

        if (book != null && book.getBookCounts() >= 1) {
            Book book1 = new Book(book.getBookID(), book.getBookName(), book.getBookCounts() - 1, book.getAuthor(), book.getBookType(), book.getPrice(), book.getBookLanguage());
            bookService.updateBook(book1);
            HttpSession session = request.getSession();
            String username = (String) session.getAttribute("username");
            System.out.println(username);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            String dateString = format.format(date);
            Date sDate = format.parse(dateString);
            Calendar cal = Calendar.getInstance();
            cal.setTime(sDate);
            cal.add(Calendar.DAY_OF_MONTH, LendTime);
            Date backDate = cal.getTime();
            LendList lendList = new LendList(2, bookName, username, date, backDate, "借书中");
            System.out.println(lendList);
            lendListService.lendBook(lendList);
            return "redirect:/lendListIndex";
        } else {
            String str="借书失败";
            model.addAttribute("str",str);
            return "redirect:/lendListIndex";
        }

    }
    @RequestMapping("/login/addLend1")
    public String addLend1(String bookName,@RequestParam(defaultValue = "7") int LendTime, HttpServletRequest request) throws ParseException {
        Book book = bookService.queryBookByName(bookName);
        int bookCounts = book.getBookCounts();
        if (book != null && bookCounts >= 1) {
            Book book1 = new Book(book.getBookID(), book.getBookName(), book.getBookCounts() - 1, book.getAuthor(), book.getBookType(), book.getPrice(), book.getBookLanguage());
            bookService.updateBook(book1);
            HttpSession session = request.getSession();
            String username = (String) session.getAttribute("username");
            System.out.println(username);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            String dateString = format.format(date);
            Date sDate = format.parse(dateString);
            Calendar cal = Calendar.getInstance();
            cal.setTime(sDate);
            cal.add(Calendar.DAY_OF_MONTH, LendTime);
            Date backDate = cal.getTime();

            LendList lendList = new LendList(2, bookName, username, date, backDate, "借书中");
            System.out.println(lendList);
            lendListService.lendBook(lendList);
            return "redirect:/bookIndex";
        } else {
            return "lendList/error";
        }
    }
    @RequestMapping("/login/exportLendList")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        List<LendList> allLendList = lendListService.getAllLendList();
        ExportExcel<LendList> ee = new ExportExcel<LendList>();
        String[] headers = {"借书编号", "书名", "借阅人", "借书日期", "还书日期", "借书状态"};
        String fileName = "阅读之家借阅excel表";
        ee.exportExcel(headers, allLendList, fileName, response);
    }

    @RequestMapping("/login/backBook")
    public ModelAndView backBook(HttpServletRequest request, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size) {
        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        List<LendList> allLendList2 = lendListService.getAllLendList2(username, page, size);
        PageInfo<LendList> pageInfo = new PageInfo<>(allLendList2);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("lendList/userLend");
        return mv;
    }
}
